﻿function calculate() {
    var num = $("#hnd_num").val();
    var price = eval("(" + $("#paper_" + $("#hnd_paper").val()).attr("p") + ")")[num];
    var corner_price = $("#corner_" + $("#hnd_corner").val()).attr("p");
    var other = 0;
    price = accAdd(price, corner_price);
    $("#spanDesc").text( $("#paper_" + $("#hnd_paper").val()).attr("d"));
    $("#price").text(price);
    $("#num").text(num);
    $("#ckbAdjust").is(":checked") ? ($("#spWT").show(), other = $("#hndAdjustPrice").val()) : ($("#spWT").hide(), other = 0)
    $("#total").text(new Number(accAdd(accMul(price, num), other)).toFixed(2));
}

function select(t, c) {
    $("." + t).each(function() {
        $(this).removeClass("show");
        if ($(this).attr("c") == c) {
            $(this).addClass("show");
            $("#select_" + t).text("\"" + $.trim($(this).text()) + "\"");
            $("#hnd_" + t).val(c);
        }
    });
    calculate();
}

function adjust() {
    
    $("#ckbAdjust").is(":checked") ? ($("#adjust-li").show()) : ($("#adjust-li").hide());
    calculate();
}

function focus_adjust() {
    $("#txtAdjust").text($("#txtAdjust").val());
    if ($.trim($("#txtAdjust").text()) == "仅限明确具体的文字排版类调整，如更换字体、调整字号大小、添加删减文字段或排版位置调整等。且微调后直接安排印刷，不再另行确认") {
        $("#txtAdjust").text("");
    }
}

function blur_adjust() {
    $("#txtAdjust").text($("#txtAdjust").val());
    if ($.trim($("#txtAdjust").text()) == "") {
        $("#txtAdjust").text("仅限明确具体的文字排版类调整，如更换字体、调整字号大小、添加删减文字段或排版位置调整等。且微调后直接安排印刷，不再另行确认");
    }
}

function add_cart(callback) {
    loading();
    $.ajax({
        type: "POST",
        url: "CardService.ashx?a=ac",
        timeout: 6000,
        data: {
            'paper': $("#hnd_paper").val(),
            'corner': $("#hnd_corner").val(),
            'num': $("#hnd_num").val(),
            'cardID': $("#hndCardID").val(),
            'cardType': $("#hndCardType").val(),
            'adjust': $("#ckbAdjust").is(":checked"),
            'adjustDesc': $("#ckbAdjust").is(":checked") ? escape($("#txtAdjust").text()) : ""
        },
        success: function(data) {
            var result = eval("(" + data + ")")
            if (result.r == "y") {            
                if (callback) {
                        callback.call(this, result);
                    } else {
                        setCart();
                        close_loading();
                        add_cart_sucess()
                   }
                }else if(result.r == "n"){
                    close_loading();
                    alert("操作失败："+ result.msg);                    
                }
        }
    });
}

function now_buy() {
    window.location = "/member/cart.aspx";
}

function add_cart_sucess() {
    if ($("#add-sucess-div").length <= 0) {
        $(document.forms[0]).append("<div id='add-sucess-div' style='display:none;text-align:center;'>"
            + "<div id='add-sucess-s-div' style='width:200px;background-color:#ffffff;'>"
            + "<span style='padding:5px;'><h2 style='margin:5px;margin-bottom:20px;'>成功加入购物车！</h2><p><a href='/member/cart.aspx'> 去结算 &gt;&gt; </a> <a href='/'>再逛逛 &gt;&gt; </a></p></span>"
            + "</div>"
            + "</div>");
    }
    shade("add-sucess-div");
    $("#add-sucess-s-div").css("margin-top", $(document).scrollTop() + ($(window).height() - $("#add-sucess-s-div").height()) / 2);
    $("#add-sucess-s-div").css("margin-left", $(document).scrollLeft() + ($(window).width() - $("#add-sucess-s-div").width()) / 2);
}